﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;

namespace MisBLL
{
    public class GoodsCusDAO : MarshalByRefObject, IGoodsCus
    {
        protected MisEntity.DataEntityDataContext Ctx = new MisEntity.DataEntityDataContext();

        public IList<MisEntity.GoodsCus> FindAll()
        {
            var query = from a in Ctx.GoodsCus
                        select a;
            if (query.Count<MisEntity.GoodsCus>() == 0)
                return null;
            return query.ToList<MisEntity.GoodsCus>();
        }

        public MisEntity.GoodsCus Find(string CusID, string ID)
        {
            var query = from a in Ctx.GoodsCus
                        where a.CUS_ID == CusID && a.ID == ID
                        select a;
            if (query.Count<MisEntity.GoodsCus>() == 0)
                return null;
            return query.First<MisEntity.GoodsCus>();
        }

        public void Create(MisEntity.GoodsCus entity)
        {
            Ctx.GoodsCus.InsertOnSubmit(entity);
            Ctx.SubmitChanges();
        }

        public MisEntity.GoodsCus Load(string Id)
        {
            var query = from item in Ctx.GoodsCus
                        where item.ID == Id
                        select item;
            if (query.Count<MisEntity.GoodsCus>() == 0)
                return null;
            return query.First<MisEntity.GoodsCus>();
        }

        public void Update(MisEntity.GoodsCus entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(string Id)
        {
            MisEntity.GoodsCus info = Ctx.GoodsCus.First<MisEntity.GoodsCus>(item => item.ID == Id);
            Ctx.GoodsCus.DeleteOnSubmit(info);
            Ctx.SubmitChanges();
        }

        public void Delete(MisEntity.GoodsCus entity)
        {
            var goodsCusList = from info in Ctx.GoodsCus
                           where info.ID == entity.ID
                           select info;
            foreach (var goodsCus in goodsCusList)
            {
                Ctx.GoodsCus.DeleteOnSubmit(goodsCus);
            }
            Ctx.SubmitChanges();
        }
    }
}
